<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="2"
        :formType="type"
        labelWidth="100"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center',
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton :click="saveDialog" type="primary">保 存</AjaxButton>
    </div>
  </div>
</template>

<script>
import { editAdExposed } from '@/api/cms-common/adexposed';
import { dictionary } from '@/utils/dictMixin';
// import { getLoginUser } from '@/api/cms-api/projectOperating/program/departmentInquire'; //获取登录信息
import { getModelAll } from '@/api/cms-common/basedata'; // 全部车型
import { findFinanceCompanySeriesList, getDealerAttachAuth } from '@/api/cms-common/basedata';

export default {
  name: 'DefeatreasonModel',
  mixins: [dictionary],
  props: {
    type: {
      type: String
    },
    initData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    this.id = null;
    return {
      formList: this.createFormList(),
      visible: false
    };
  },
  created() {
    this.init();
    this.getModelAll();
  },
  methods: {
    init() {
      // 更新接口需要的记录 ID
      //this.recordId = this.initData.defeatReasonId;
      //this.dataSources = this.initData.dataSources;

      this.id = this.initData['id'];
      this.formList.forEach(x => {
        x.initialValue = this.initData[x.fieldName];
      });
    },
    createFormList() {
      return [
        {
          //标签
          type: 'BREAK_SPACE',
          label: '基本信息',
          style: {}
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'ownerCode',
          placeholder: '请输入',
          maxlength: 30,
          disabled: true,
          style: { width: '200px' },
          rules: [{ required: true, message: '请填写战败原因', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '经销商全称',
          maxlength: 120,
          fieldName: 'dealerName',
          disabled: true,
          placeholder: '请输入',
          style: { width: '200px' }
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'status',
          disabled: true,
          style: { width: '200px' },
          initialValue: 10031001,
          itemList: this.createDictList('1003')
        },
        {
          type: 'SELECT',
          label: '是否广告露出',
          fieldName: 'exposed',
          placeholder: '请选择',
          style: { width: '200px' },
          initialValue: 10041001,
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '请选择是否有效', trigger: 'blur' }]
        },
        {
          //标签
          type: 'BREAK_SPACE',
          label: '燃油车授权',
          style: {}
        },
        {
          type: 'CHECKBOX',
          label: '燃油车',
          selfCols: 3,
          fieldName: 'financeCompanyIdOld',
          disabled: true,
          initialValue: [],
          itemList: [] //复选框数据源
        },
        {
          type: 'MULTIPLE_CHECKBOX',
          label: '普通授权车型',
          selfCols: 3,
          fieldName: 'authorizedModels1',
          change: this.modelChange1,
          initialValue: [],
          itemList: [] //复选框数据源
        },

        {
          type: 'MULTIPLE_CHECKBOX',
          label: '特殊授权车型',
          selfCols: 3,
          fieldName: 'authorizedModels2',
          change: this.modelChange2,
          initialValue: [],
          itemList: [] //复选框数据源
        },
        {
          //标签
          type: 'BREAK_SPACE',
          label: '新能源授权',
          style: {}
        },
        {
          type: 'CHECKBOX',
          label: '新能源',
          disabled: true,
          selfCols: 3,
          fieldName: 'financeCompanyIdNew',
          initialValue: [],
          itemList: [] //复选框数据源
        },
        {
          type: 'MULTIPLE_CHECKBOX',
          label: '普通授权车型',
          selfCols: 3,
          fieldName: 'authorizedModels3',
          change: this.modelChange3,
          initialValue: [],
          itemList: [] //复选框数据源
        },
        {
          type: 'MULTIPLE_CHECKBOX',
          label: '特殊授权车型',
          selfCols: 3,
          fieldName: 'authorizedModels4',
          change: this.modelChange4,
          initialValue: [],
          itemList: [] //复选框数据源
        }
      ];
    },
    //获取所有车型数据
    async getModelAll() {
      this.formList.forEach(x => {
        if (x.fieldName == 'ownerCode') {
          this.ownerCode = x.initialValue;
        }
      });

      // this.loginUser = await getLoginUser({});
      let attachModelRes = await getModelAll();
      let findFinanceCompanySeries = await findFinanceCompanySeriesList();
      let dealerAttachAuth = await getDealerAttachAuth({ ownerCode: this.ownerCode });
      let dealerAttachAuthData = null;
      if (dealerAttachAuth.resultCode === 200) {
        let financeCompanyIdOld = this.formList.find(item => item.fieldName === 'financeCompanyIdOld'); //燃油车
        let financeCompanyIdNew = this.formList.find(item => item.fieldName === 'financeCompanyIdNew'); //新能源
        financeCompanyIdOld.initialValue = false;
        financeCompanyIdNew.initialValue = false;
        dealerAttachAuthData = dealerAttachAuth.data;
        dealerAttachAuthData.forEach(data => {
          let seriesData = null;
          if (findFinanceCompanySeries.resultCode === 200) {
            seriesData = findFinanceCompanySeries.data;
          }
          if (seriesData == null) {
            //没有维护车系 tr_finance_company_series 表的数据
            return;
          }

          if (attachModelRes.resultCode === 200) {
            if (attachModelRes.data) {
              let inModelListOld = [];
              let inModelListNew = [];
              let mapModel = {};
              let financeCompanyId = null;
              this.authorizedModels1 = this.formList.find(item => item.fieldName === 'authorizedModels1'); //普通
              this.authorizedModels2 = this.formList.find(item => item.fieldName === 'authorizedModels2'); //特殊
              this.authorizedModels3 = this.formList.find(item => item.fieldName === 'authorizedModels3'); //普通
              this.authorizedModels4 = this.formList.find(item => item.fieldName === 'authorizedModels4'); //特殊

              attachModelRes.data.forEach(x => {
                seriesData.forEach(v => {
                  //匹配型号对应的经销形式
                  if (v.seriesId == x.seriesId) {
                    financeCompanyId = v.financeCompanyId;
                    return;
                  }
                });
                //车型是燃油 并且 经销商上也授权了燃油
                if (financeCompanyId == '2' && data.financeCompanyId == financeCompanyId) {
                  //燃油车
                  financeCompanyIdOld.initialValue = true;
                  this.authorizedModels1.itemList.push({ text: x.modelName, value: x.modelId }); //普通
                  this.authorizedModels2.itemList.push({ text: x.modelName, value: x.modelId }); //特殊
                } else {
                  //新能源
                  if (data.financeCompanyId == financeCompanyId) {
                    //经销商授权了新能源
                    financeCompanyIdNew.initialValue = true;
                    this.authorizedModels3.itemList.push({ text: x.modelName, value: x.modelId }); //普通
                    this.authorizedModels4.itemList.push({ text: x.modelName, value: x.modelId }); //特殊
                  }
                }
              });
            }
          }
        });
      } else {
        //当前经销商没有对经销形式授权过
      }
    },
    //燃油-普通车型-变更
    modelChange1(val) {
      //取消勾选
      this.authorizedModels1.initialValue = [];
      val.find(x => {
        let isContinue = true; //是否车型重复
        this.authorizedModels2.initialValue.find(v => {
          if (x == v) {
            isContinue = false;
            this.$notify.error({ title: '提示信息', message: '您好，特殊车型与普通车型不可重复选择！' });
          }
        });
        if (isContinue) {
          this.authorizedModels1.initialValue.push(x);
        }
      });
    },
    //燃油-特殊车型-变更
    modelChange2(val) {
      //取消勾选
      this.authorizedModels2.initialValue = [];
      val.find(x => {
        let isContinue = true; //是否车型重复
        this.authorizedModels1.initialValue.find(v => {
          if (x == v) {
            isContinue = false;
            this.$notify.error({ title: '提示信息', message: '您好，特殊车型与普通车型不可重复选择！' });
          }
        });
        if (isContinue) {
          this.authorizedModels2.initialValue.push(x);
        }
      });
    },
    //新能源-普通车型-变更
    modelChange3(val) {
      //取消勾选
      this.authorizedModels3.initialValue = [];
      val.find(x => {
        let isContinue = true; //是否车型重复
        this.authorizedModels4.initialValue.find(v => {
          if (x == v) {
            isContinue = false;
            this.$notify.error({ title: '提示信息', message: '您好，特殊车型与普通车型不可重复选择！' });
          }
        });
        if (isContinue) {
          this.authorizedModels3.initialValue.push(x);
        }
      });
    },
    //新能源-特殊车型-变更
    modelChange4(val) {
      //取消勾选
      this.authorizedModels4.initialValue = [];
      val.find(x => {
        let isContinue = true; //是否车型重复
        this.authorizedModels3.initialValue.find(v => {
          if (x == v) {
            isContinue = false;
            this.$notify.error({ title: '提示信息', message: '您好，特殊车型与普通车型不可重复选择！' });
          }
        });
        if (isContinue) {
          this.authorizedModels4.initialValue.push(x);
        }
      });
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      this.saveData = Object.assign(this.saveData, { id: this.id });
      if (this.saveData !== null) {
        let res = await editAdExposed(this.saveData);
        console.log(res, '12345675434567u');
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
          this.$message.success('保存成功!');
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', param);
      this.visible = false;
    }
  }
};
</script>

<style lang="less" scoped>
</style>
